package net.mikoo.seals.api.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.api.model.ResultType;
import net.mikoo.seals.api.model.company.CompanyListResponse;
import net.mikoo.seals.api.model.company.CurrentResponse;
import net.mikoo.seals.api.model.company.SwitchCompanyRequest;
import net.mikoo.seals.api.utility.ApiConstants;
import net.mikoo.seals.api.utility.PartyUtility;
import net.mikoo.seals.biz.service.CompanyService;
import net.mikoo.seals.model.Company;
import net.mikoo.seals.model.Party;

@Controller
public class CompanyController {

	private final static Logger logger = LoggerFactory.getLogger(CompanyController.class);
	
	@Autowired
	private CompanyService companyService;

	@Autowired
	private RedisTemplate<String, Object> objectRedisTemplate;
	
	@RequestMapping("/company/current")
	@ResponseBody
	public CurrentResponse current(HttpServletRequest request) {
		CurrentResponse result = new CurrentResponse();
		try {
			
			Party party = PartyUtility.getCurrentParty(request);
			if(party == null) {
				throw new IllegalStateException("not login");
			}
			
			Company company = party.getCompany();
			if(company == null) {
				List<Company> companyList = companyService.getListByPartyId(party.getPartyId());
				if(companyList != null && companyList.size() == 0) {
					company = companyList.get(0);
				}
			}
			
			result = CurrentResponse.valueOf(company);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
	@RequestMapping("/company/list")
	@ResponseBody
	public CompanyListResponse list(HttpServletRequest request) {
		CompanyListResponse result = new CompanyListResponse();
		try {
			List<Company> companyList = companyService.getListByPartyId(PartyUtility.getCurrentPartyId(request));
			result = CompanyListResponse.valueOf(companyList);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
	@RequestMapping("/company/switch")
	@ResponseBody
	public CurrentResponse switchCompany(SwitchCompanyRequest request, HttpServletRequest httpRequest) {
		CurrentResponse result = new CurrentResponse();
		try {
			
			if(request == null) {
				throw new IllegalArgumentException("request");
			}
			
			if(request.getCompanyId() == null) {
				throw new IllegalArgumentException("request.companyId");
			}
			
			Party party = PartyUtility.getCurrentParty(httpRequest);
			if(party == null) {
				throw new IllegalStateException("not login");
			}
			
			Company company = party.getCompany();
			if(company != null && !company.getCompanyId().equals(request.getCompanyId())) {
				Company existCompany = companyService.getDetail(request.getCompanyId());
				if(existCompany == null) {
					throw new IllegalStateException("company not exist");
				}
				company = existCompany;
				party.setCompany(existCompany);
				party.setCompanyId(company.getCompanyId());
				objectRedisTemplate.opsForValue().set(ApiConstants.MPI_OAUTH_TOKEN_TAG + PartyUtility.getToken(httpRequest), party);
			}
			
			result = CurrentResponse.valueOf(company);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
}
